package sql_age;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.db.DBConfiguration;
import org.apache.hadoop.mapreduce.lib.db.DBInputFormat;
import org.apache.hadoop.mapreduce.lib.db.DBOutputFormat;
import org.apache.log4j.BasicConfigurator;

public class Driver {
    public static void main(String[] args) throws Exception {
        BasicConfigurator.configure();
        Configuration conf = new Configuration();

        DBConfiguration.configureDB(conf, "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/data", "root", "200028");
        Job job = Job.getInstance(conf);
        job.setJarByClass(Driver.class);

        job.setMapperClass(MapTest.class);
        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setReducerClass(RedTest.class);
        job.setOutputKeyClass(result.class);
        job.setOutputValueClass(NullWritable.class);

        String[] f1 = {"id", "sid", "name", "age", "sex", "birthday"};
        String[] f2 = {"age", "count"};
        DBInputFormat.setInput(job, Student.class, "Student", null, "sid", f1);
        DBOutputFormat.setOutput(job, "result", f2);
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
